package lk.mrt.cse.wihidum.receiver;

import org.apache.axis2.receivers.AbstractMessageReceiver;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.AxisFault;
import org.apache.log4j.Logger;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.om.OMException;

import lk.mrt.cse.wihidum.util.WihidumConstants;
import lk.mrt.cse.wihidum.util.SelfReader;
import lk.mrt.cse.wihidum.processor.PublicationProcessor;
import lk.mrt.cse.wihidum.processor.SubscriptionProcessor;
import lk.mrt.cse.wihidum.processor.Processor;

//import java.util.UUID;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Chami
 * Date: Mar 7, 2010
 * Time: 6:11:17 PM
 * To change this template use File | Settings | File Templates.
 */
public class InOnlyMessageReceiver extends AbstractMessageReceiver
{
    private static final Logger logger = Logger.getLogger(InOnlyMessageReceiver.class);
    private Processor publicationProcessor;
    HashMap<String, String> brokerMap;
    long i = 0;
    
     public InOnlyMessageReceiver ()
    {
        publicationProcessor = new PublicationProcessor();
        brokerMap = (HashMap<String, String>) SelfReader.read();
    }
    
    protected void invokeBusinessLogic(MessageContext inMessageContext) throws AxisFault
    {
        try
        {
            SOAPEnvelope inSoapEnvelope = inMessageContext.getEnvelope();
            String action = inMessageContext.getOptions().getAction();

            if(action.equals(WihidumConstants.Operations.PUBLISH))
            {
                i++;
                String uuid= brokerMap.get("id").concat("_"+ i);
                logger.info ("An event received with ID#"+uuid);
                // Hand over the message to the publication processor
                publicationProcessor.processMessage (inSoapEnvelope, uuid);
            }
        }
        catch (NullPointerException ex)
        {
            logger.error ("Error in recognizing received message format", ex);
        }
        catch (OMException ex)
        {
            logger.error ("Error in recognizing received message format", ex);
        }      
    }
}
